package dexter.core.unit;

import static org.junit.Assert.assertTrue;

import java.util.Properties;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import dexter.core.client.Client;
import dexter.core.client.LocalClient;
import dexter.core.client.RemoteClient;
import dexter.core.coord.Coordinator;
import dexter.exception.RemoteTestException;

import dexter.util.PropertiesReader;

public class RemoteClientTest {
	
	private Coordinator coord;
	
	private Client remoteClient;
	private Properties props;
	
	@Before
	public void init() {
		
		props = PropertiesReader.getDefaultProperties();
		coord = new Coordinator(props);
		String host =  props.getProperty("coord_host");
		int port =  Integer.parseInt(props.getProperty("coord_port"));
		remoteClient = new LocalClient(host,port);
	}
	
	
	@Test
	public void testClientCoordPhase2() {
		
		coord.start();
		
		try {Thread.sleep(2000);} catch(Exception e) {}
		
		try {
			props.setProperty("classname", "core.ServerInterfaceImplPhase2");
			props.setProperty("election", "false");
			remoteClient.executeTest(props);
		} catch (RemoteTestException e) {
			e.printStackTrace();

			assertTrue(false);
		}
		
        assertTrue(true);
		
	}
	
	@Ignore
	@Test
	public void testClientCoordPhase3() {
		
		coord.start();
		
		try {Thread.sleep(2000);} catch(Exception e) {}
		
		try {
			props.setProperty("classname", "core.ServerInterfaceImplPhase3");
			props.setProperty("election", "true");
			remoteClient.executeTest(props);
		} catch (RemoteTestException e) {
			e.printStackTrace();

			assertTrue(false);
		}
		
        assertTrue(true);
		
	}
	
	@Ignore
	@Test
	public void testFiveClients() {
		
		coord.start();
		
		try {Thread.sleep(2000);} catch(Exception e) {}
		
		for (int i=0; i<5; i ++) {
			try {
			remoteClient.executeTest(props);
			} catch (RemoteTestException e) {
				e.printStackTrace();
			assertTrue(false);
			}
		}
        assertTrue(true);
		
	}
	
}
